package com.bdqn.tour.dao.impl;



import com.bdqn.tour.dao.BaseDao;
import com.bdqn.tour.dao.HzyLocationDao;
import com.bdqn.tour.pojo.HzyLocation;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HzyLocationDaoImpl extends BaseDao implements HzyLocationDao {
    @Override
    public boolean addLocation(HzyLocation location) {
        try {
            conn = getConnection();
            String sql = "INSERT INTO Location(receiverName, province, city, district, streetAddress, postalCode,areaCode,phoneNumber,extension, mobilePhone) VALUES(?,?,?,?,?,?,?,?,?,?)";

            int result = executeUpdate(sql,
                    location.getReceiverName(),
                    location.getProvince(),
                    location.getCity(),
                    location.getDistrict(),
                    location.getStreetAddress(),
                    location.getPostalCode(),
                    location.getAreaCode(),
                    location.getPhoneNumber(),
                    location.getAreaCode(),
                    location.getMobilePhone()
            );
            return result > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean deleteLocation(int id) {
        try {
            conn = getConnection();
            String sql = "DELETE FROM Location WHERE id=?";
            int result = executeUpdate(sql, id);
            return result > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
    }

    @Override
    public boolean updateLocation(HzyLocation location) {
        try {
            conn = getConnection();
            String sql = "UPDATE Location SET receiverName=?, province=?, city=?, district=?, streetAddress=?, postalCode=?, areaCode=?, phoneNumber=?, extension=?, mobilePhone=? WHERE id=?";
            int result = executeUpdate(sql,
                    location.getReceiverName(),
                    location.getProvince(),
                    location.getCity(),
                    location.getDistrict(),
                    location.getStreetAddress(),
                    location.getPostalCode(),
                    location.getAreaCode(),
                    location.getPhoneNumber(),
                    location.getAreaCode(),
                    location.getMobilePhone(),
                    location.getId()
            );

            return result > 0;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
    }

    @Override
    public List<HzyLocation> getLocationList(int pageNo, int pageSize) {
        List<HzyLocation> locations = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "SELECT * FROM Location LIMIT ?,?";

            rs = executeQuery(sql, (pageNo - 1) * pageSize, pageSize);

            while (rs.next()) {
                HzyLocation location = new HzyLocation();
                location.setId(rs.getInt("id"));
                location.setReceiverName(rs.getString("receiverName"));
                location.setProvince(rs.getString("province"));
                location.setCity(rs.getString("city"));
                location.setDistrict(rs.getString("district"));
                location.setStreetAddress(rs.getString("streetAddress"));
                location.setPostalCode(rs.getString("postalCode"));
                location.setAreaCode(rs.getString("areaCode"));
                location.setPhoneNumber(rs.getString("phoneNumber"));
                location.setExtension(rs.getString("extension"));
                location.setMobilePhone(rs.getString("mobilePhone"));
                locations.add(location);

            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
        return locations;
    }

    @Override
    public int getTotalCount() {
        int totalCount = 0;
        try {
            conn = getConnection();
            String sql = "SELECT COUNT(*) FROM Location";
            rs = executeQuery(sql);
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
        return totalCount;
    }

    @Override
    public HzyLocation getLocationById(int id) {
        HzyLocation location = null;
        try {
            conn = getConnection();
            String sql = "SELECT * FROM Location WHERE id=?";

            rs = executeQuery(sql, id);
            if (rs.next()) {
                location = new HzyLocation();
                location.setId(rs.getInt("id"));
                location.setReceiverName(rs.getString("receiverName"));
                location.setProvince(rs.getString("province"));
                location.setCity(rs.getString("city"));
                location.setDistrict(rs.getString("district"));
                location.setStreetAddress(rs.getString("streetAddress"));
                location.setPostalCode(rs.getString("postalCode"));
                location.setAreaCode(rs.getString("areaCode"));
                location.setPhoneNumber(rs.getString("phoneNumber"));
                location.setExtension(rs.getString("extension"));
                location.setMobilePhone(rs.getString("mobilePhone"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(conn, psmt, rs);
        }
        return location;
    }
}
